#!/usr/bin/env python3

import csv
import yaml
import os
import shutil
import re
from collections import defaultdict

'''
# 推荐使用 Python 3.12.8
pip install PyYAML pandas numpy

# 输入表格格式: cvs
# 字号	标识符	中文	英文	俄语	德语	法语

2025.3.8  非ASCII字符统计维度: 使用元组（字号，字体粗细）作为键（增加字体粗细）
2025.3.10 增加生成所有字符的表格

你现在是一个资深的翻译官，你会将我给出的中文，翻译为其他语言的文字
我将给出cvs格式的数据，格式对于如下
字号,字体,中文
你要将这种格式排列的数据中的中文翻译为这种格式：
字号,字体,标识符,中文,英文,俄语,德语,法语
以上格式中的标识符是通过英文生成而来，标识符需要简洁明了，要符合C语言的标识符格式,他们是所有字符都是小写

一下是输入表格格式: cvs

'''

input_file = r'./hichip.csv'
makefile_path = r'./Makefile'

output_files = {
    'en-GB.yml': 'English',
    'zh-CN.yml': 'Chinese',
    'ru-RU.yml': 'Russian',
    'de-DE.yml': 'German',
    'fr-FR.yml': 'French'
}
output_folder = r'./translations'

font_style_to_non_ascii = defaultdict(set)
font_style_to_all_chars = defaultdict(set)

def read_csv(input_file):
    with open(input_file, 'r', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)
        rows = list(reader)
    return rows

def generate_yaml_for_language(rows, lang, output_file):
    lang_code = output_file.split('.')[0]
    data = {lang_code: {}}

    for row in rows:
        # 确保使用正确的列索引
        font_size = row[0]     # 第1列：字号
        font_weight = row[1]   # 第2列：字体粗细
        identifier = row[2]    # 第3列：标识符

        key_name = f's_{identifier}_{font_weight}_{font_size}'

        lang_col_map = {
            'Chinese': 3,
            'English': 4,
            'Russian': 5,
            'German': 6,
            'French': 7
        }
        translated_text = row[lang_col_map[lang]]

        extract_non_ascii_characters(font_size, font_weight, translated_text)
        extract_all_characters(font_size, font_weight, translated_text)

        data[lang_code][key_name] = translated_text.rstrip()

    return data

def extract_non_ascii_characters(font_size, font_weight, text):
    non_ascii = re.findall(r'[^\x00-\x7F]+', text)
    if non_ascii:
        key = (font_size, font_weight)
        for char in non_ascii:
            font_style_to_non_ascii[key].update(char)

def extract_all_characters(font_size, font_weight, text):
    key = (font_size, font_weight)
    for char in text:
        font_style_to_all_chars[key].add(char)

def write_non_ascii_csv(output_folder):
    output_file = os.path.join(output_folder, 'non_ascii_characters.csv')
    with open(output_file, 'w', encoding='utf-8', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Font Size', 'Font Weight', 'Non-ASCII Character'])

        for (font_size, font_weight), chars in font_style_to_non_ascii.items():
            unique_chars = ''.join(sorted(chars))
            writer.writerow([font_size, font_weight, unique_chars])
    print(f'Generate CSV tables: {output_file}')

def write_all_characters_csv(output_folder):
    output_file = os.path.join(output_folder, 'all_characters.csv')
    with open(output_file, 'w', encoding='utf-8', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['Font Size', 'Font Weight', 'All Characters'])

        for (font_size, font_weight), chars in font_style_to_all_chars.items():
            unique_chars = ''.join(sorted(chars))
            writer.writerow([font_size, font_weight, unique_chars])
    print(f'Generate CSV tables: {output_file}')

def write_yaml_file(output_file, data):
    with open(output_file, 'w', encoding='utf-8') as ymlfile:
        yaml.dump(data, ymlfile, allow_unicode=True, default_flow_style=False)

def ensure_output_folder_exists(output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        print(f'make new dir: {output_folder}')

def copy_makefile(output_folder):
    if os.path.exists(makefile_path):
        shutil.copy(makefile_path, os.path.join(output_folder, 'Makefile'))
        print(f"{makefile_path} copy to {output_folder}")

def main():
    print(f"WorkSpace: {os.getcwd()}")
    rows = read_csv(input_file)
    ensure_output_folder_exists(output_folder)

    for output_file, lang in output_files.items():
        data = generate_yaml_for_language(rows, lang, output_file)
        yml_output_path = os.path.join(output_folder, output_file)
        write_yaml_file(yml_output_path, data)
        print(f'Generate file: {yml_output_path}')

    write_non_ascii_csv(output_folder)
    write_all_characters_csv(output_folder)
    copy_makefile(output_folder)

if __name__ == '__main__':
    main()